/** @noSelfInFile **/

import {ItemDataKey, ItemSourceData} from "../../../dataSource/DataSource";
import {ItemJson} from "../../model/StoreJson";
import {LocModel} from "../../base/LocModel";
import {CodeUtil} from "../../../codeAdapter/CodeUtil";
import {ItemData} from "../../../dataSource/DataModel";
import {HandleModel} from "../model1/HandleModel";
import {IItemAdapter, itemAdapter} from "../../adapter/adapterInterface/model3/IItemAdapter";
import {GlobalModel} from "../model1/GlobalModel";

// todo 【需要被回退】
// export class ItemModel implements HandleModel {
//     public data: ItemSourceData = new ItemSourceData();
//     public unit: UnitModel;
//     public template: ItemData;
//
//     id: string;
//     nikeName: string;
//
//     constructor(name: string) {
//         this.nikeName = name;
//     }
//
//     createHandle(itemJson: ItemJson, template: ItemData, loc: LocModel) {
//         if (itemJson) {
//             this.unit = new UnitModel(this.nikeName);
//             const neutralInvincibleId = globalAdapter.adapter.getNeutralInvinciblePlayerId();
//             const itemUnitId = Configer.getFinal(FinalKey.itemUnitId);
//
//             const itemUnitData = DataRouter.getUnitDataByUnitId(itemUnitId);
//             const unitJson: UnitJson = { id: itemUnitId };
//             this.unit.createHandle(neutralInvincibleId, unitJson, itemUnitData, loc, 0);
//
//             this.bindHandle(template);
//             this.loadJson(itemJson)
//         }
//     }
//
//     public get charges() {
//         return this.data[ItemDataKey.num]
//     }
//
//     public get af() {
//         return this.data[ItemDataKey.itemAffix]
//     }
//
//     public set charges(value: number) {
//         this.data[ItemDataKey.num] = value
//     }
//
//     public get show() {
//         return this.unit.show
//     }
//
//     public set show(show: boolean) {
//         this.unit.show = show;
//     }
//
//     get name() {
//         return this.template.Name;
//     }
//
//     public get strId() {
//         return this.template.id;
//     }
//
//     public isAlive() {
//         return this.unit.isAlive()
//     }
//
//     public get loc() {
//         return this.unit.loc;
//     }
//
//     public set loc(locModel: LocModel) {
//         this.unit.loc = locModel;
//     }
//
//     public isPawnable() {
//         return !CodeUtil.isNullOrZero(this.data[ItemDataKey.pawnable])
//     }
//
//     public isPowerup() {
//         return !CodeUtil.isNullOrZero(this.data[ItemDataKey.powerup])
//     }
//
//     public setDropOnDeath(flag: boolean) {
//         this.data[ItemDataKey.dropInDeath] = flag ? 1 : 0;
//     }
//
//     public setDroppable(flag: boolean) {
//         this.data[ItemDataKey.dropAble] = flag ? 1 : 0;
//     }
//
//     public getJson() {
//         const json: ItemJson = { id: this.strId, n: this.charges, af: this.data[ItemDataKey.itemAffix], cd: this.data[ItemDataKey.coolDown] };
//         return json
//     }
//     public loadJson(json: ItemJson) {
//         if (!CodeUtil.isNullOrZero(json.n)) {
//             this.charges = json.n;
//         }
//         if (!CodeUtil.isNullOrZero(json.af)) {
//             this.data[ItemDataKey.itemAffix] = json.af;
//         }
//     }
//
//     destroy() {
//         this.unit.destroy()
//     }
//
//     bindHandle(template?: ItemData) {
//         this.template = template;
//         this.id = this.unit.getHandleId();
//     }
//
//     equal(handle: ItemModel): boolean {
//         return this.unit.equal(handle.unit)
//     }
//
//     getHandleId(): string {
//         return this.id;
//     }
//
//     isValid(): boolean {
//         return  this.unit.isValid()
//     }
//
//     giveToUnit(unit: UnitModel) {
//         // this.data[ItemDataKey.onUnit] = unit;
//         this.show = false;
//     }
//
//     dropToLoc(loc: LocModel) {
//         this.show = true;
//         // this.data[ItemDataKey.onUnit] = undefined;
//         this.loc = loc;
//     }
//
//     get cd() {
//         return this.data[ItemDataKey.coolDown]
//     }
//
// }


/** @noSelfInFile **/


export class ItemModel implements HandleModel {
    public data: ItemSourceData = new ItemSourceData();
    public template: ItemData;
    itemAdapter: IItemAdapter;

    public handle: any;
    id: string;
    nikeName: string;

    constructor(name: string) {
        this.nikeName = name;
        this.itemAdapter = itemAdapter.adapter;
    }

    createHandle(itemJson: ItemJson, template: ItemData, loc: LocModel) {
        if (itemJson) {
            const id = itemJson.id;
            const handle = this.itemAdapter.createHandle(id, loc)
            this.bindHandle(handle, template);
            this.loadJson(itemJson)
        }
    }

    public get charges() {
        return this.itemAdapter.getCharges(this.handle);
    }

    public set charges(value: number) {
        this.itemAdapter.setCharges(this.handle, value);
    }

    public set invulnerable(flag: boolean) {
        this.itemAdapter.setInvulnerable(this.handle, flag);
    }

    public get invulnerable() {
        return this.itemAdapter.getInvulnerable(this.handle);
    }

    public get show() {
        return this.itemAdapter.getShow(this.handle)
    }

    public set show(show: boolean) {
        this.itemAdapter.setShow(this.handle, show)
    }

    get name() {
        return this.itemAdapter.getName(this.handle)
    }

    public get strId() {
        return this.itemAdapter.getStrId(this.handle)
    }


    public get life() {
        if (this.handle) {
            return this.itemAdapter.getLife(this.handle)
        } else {
            return 0;
        }
    }

    public set life(value: number) {
        this.itemAdapter.setLife(this.handle, value)
    }

    public isAlive() {
        return this.itemAdapter.isAlive(this.handle)
    }

    public get loc() {
        return this.itemAdapter.getLoc(this.handle)
    }

    public set loc(locModel: LocModel) {
        this.itemAdapter.setLoc(this.handle, locModel)
    }

    public isPawnable() {
        return this.itemAdapter.isPawnable(this.handle)
    }

    public isPowerup() {
        return this.itemAdapter.isPowerup(this.handle)
    }

    public setDropOnDeath(flag: boolean) {
        this.itemAdapter.setDropOnDeath(this.handle, flag)
    }

    public setDroppable(flag: boolean) {
        this.itemAdapter.setDroppable(this.handle, flag)
    }

    public getJson() {
        const json: ItemJson = { id: this.strId, lv: this.data[ItemDataKey.forgLv], n: this.charges, af: this.data[ItemDataKey.itemAffix], cd: this.data[ItemDataKey.coolDown] };
        return json
    }
    public loadJson(json: ItemJson) {
        if (!CodeUtil.isNullOrZero(json.n)) {
            this.charges = json.n;
        }
        if (!CodeUtil.isNullOrZero(json.lv)) {
            this.data[ItemDataKey.forgLv] = json.lv;
        }
        if (!CodeUtil.isNullOrZero(json.af)) {
            this.data[ItemDataKey.itemAffix] = json.af;
        }
    }


    destroy() {
        this.itemAdapter.destroy(this.handle)
        this.handle = undefined;
    }

    bindHandle(handle: any, template?: ItemData) {
        this.handle = handle;
        this.template = template;
        this.itemAdapter.bindHandle(handle)
        this.id = GlobalModel.getHandleId(this.handle);
    }

    equal(handle: ItemModel): boolean {
        return this.itemAdapter.equal(this.handle, handle.handle)
    }

    getHandleId(): string {
        return this.id;
    }

    isValid(): boolean {
        return  this.itemAdapter.isValid(this.handle)
    }

    dropToLoc(loc: LocModel): void {
        this.loc = loc;
    }

}
